﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace MidSoft.Sys
{
    public partial class RolePermission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                    InitRose(Request.QueryString["ID"]);
            }
        }

        private void InitRose(string id)
        {
            string SQLString = null;
            int MkFlag = 0;
            BLL_tb_MidSoft_Roles bllRoles = new BLL_tb_MidSoft_Roles();
            Model_tb_MidSoft_Roles modelRoles = new Model_tb_MidSoft_Roles();
            modelRoles = bllRoles.GetModel(ChinaSoft.Common.Input.SetInt(id));
            if (modelRoles != null && !string.IsNullOrEmpty(modelRoles.RoleName))
                RoleName.Text = modelRoles.RoleName;

            BLL_tb_MidSoft_PowerList bllPowerList = new BLL_tb_MidSoft_PowerList();
            this.cblMKName.DataSource = bllPowerList.GetList("FatherID=0");
            cblMKName.DataBind();

            //
            DataTable DT = new DataTable();
            BLL_tb_MidSoft_RoleHavePower bllRoleHavePower = new BLL_tb_MidSoft_RoleHavePower();
            DT = bllRoleHavePower.GetList("RoleId=" + id + " order by RoleHavePowerId desc").Tables[0];
            for (int i = 0; i < cblMKName.Items.Count; i++)
            {
                for (int mkI = 0; mkI < DT.Rows.Count; mkI++)
                {
                    if (cblMKName.Items[i].Value.Trim() == DT.Rows[mkI]["PowerId"].ToString().Trim())
                    {
                        cblMKName.Items[i].Selected = true;
                        MkFlag = MkFlag + 1;
                    }
                }
            }

            if (MkFlag > 0)
            {
                int MKCount = 0;
                for (int i = 0; i < cblMKName.Items.Count; i++)
                {
                    if (cblMKName.Items[i].Selected)
                    {
                        MKCount = MKCount + 1;
                        if (MKCount == 1)
                            SQLString +=" FatherID=" + cblMKName.Items[i].Value;
                        else
                            SQLString +=" OR FatherID=" + cblMKName.Items[i].Value;
                    }
                }
                cblCDName.DataSource = bllPowerList.GetList(SQLString);
                cblCDName.DataBind();
            }

            for (int i = 0; i < cblCDName.Items.Count; i++)
            {
                for (int cdI = 0; cdI < DT.Rows.Count; cdI++)
                {
                    if (cblCDName.Items[i].Value == DT.Rows[cdI]["PowerId"].ToString().Trim())
                        cblCDName.Items[i].Selected = true;
                }
            }
        }

        private void InitCD()
        {
            string SQLString = null;
            int ItemCount = 0;

            BLL_tb_MidSoft_PowerList bllPowerList = new BLL_tb_MidSoft_PowerList();

            for (int i = 0; i < this.cblMKName.Items.Count; i++)
            {
                if (this.cblMKName.Items[i].Selected)
                {
                    ItemCount = ItemCount + 1;
                    if (ItemCount == 1)
                        SQLString += " FatherID = " + this.cblMKName.Items[i].Value.Trim();
                    else
                        SQLString += " OR FatherID = " + this.cblMKName.Items[i].Value.Trim();
                }
            }
            if (ItemCount > 0)
            {
                this.cblCDName.DataSource = bllPowerList.GetList(SQLString);
                this.cblCDName.DataBind();
            }
            else
            {

                this.cblCDName.DataSource = bllPowerList.GetList(" FatherID=-1");
                this.cblCDName.DataBind();
            }
        }

        public struct DataCollectiveQX
        {
            public int CountMK;
            public int CountCD;
            public string[] MKID;
            public string[] CDID;
        }

        private DataCollectiveQX RecordRoleSet()
        {
            DataCollectiveQX DColl = new DataCollectiveQX();
            DColl.CountMK = 0;
            DColl.CountCD = 0;
            DColl.MKID = new string[cblMKName.Items.Count];
            DColl.CDID = new string[cblCDName.Items.Count];

            for (int i = 0; i < cblMKName.Items.Count; i++)
            {
                if (cblMKName.Items[i].Selected)
                {
                    DColl.MKID[DColl.CountMK] = cblMKName.Items[i].Value.Trim();
                    DColl.CountMK = DColl.CountMK + 1;
                }
            }

            for (int i = 0; i < cblCDName.Items.Count; i++)
            {
                if (cblCDName.Items[i].Selected)
                {
                    DColl.CDID[DColl.CountCD] = cblCDName.Items[i].Value.Trim();
                    DColl.CountCD = DColl.CountCD + 1;
                }
            }

            return DColl;
        }

        protected void cblMKName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataCollectiveQX DColl = RecordRoleSet();
            InitCD();

            for (int i = 0; i < DColl.CountCD; i++)
            {
                for (int cdI = 0; cdI < cblCDName.Items.Count; cdI++)
                {
                    if (cblCDName.Items[cdI].Value.Trim() == DColl.CDID[i].Trim())
                        cblCDName.Items[cdI].Selected = true;
                }
            }
        }

        protected void SavePopedom_ServerClick(object sender, EventArgs e)
        {
            string SQLString = null;

            DataTable DT = new DataTable();
            BLL_tb_MidSoft_RoleHavePower bllRoleHavePower = new BLL_tb_MidSoft_RoleHavePower();
            Model_tb_MidSoft_RoleHavePower modelRoleHavePower = new Model_tb_MidSoft_RoleHavePower();
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.DeleteList("tb_MidSoft_RoleHavePower", "RoleId", Request.QueryString["id"].ToString());

            for (int mkI = 0; mkI < cblMKName.Items.Count; mkI++)
            {
                if (this.cblMKName.Items[mkI].Selected)
                {
                    modelRoleHavePower.PowerId = ChinaSoft.Common.Input.SetInt(cblMKName.Items[mkI].Value);
                    modelRoleHavePower.RoleId = ChinaSoft.Common.Input.SetInt(Request.QueryString["id"]);
                    modelRoleHavePower.CreateTime = DateTime.Now;
                    modelRoleHavePower.CreateUserId = 0;
                    if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                        modelRoleHavePower.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);

                    int RtnInsMKBool = bllRoleHavePower.Add(modelRoleHavePower);
                    BLL_tb_MidSoft_PowerList bllPowerList = new BLL_tb_MidSoft_PowerList();
                    DT = bllPowerList.GetList("FatherID="+this.cblMKName.Items[mkI].Value).Tables[0];
                    for (int dtI = 0; dtI < DT.Rows.Count; dtI++)
                    {
                        for (int cdI = 0; cdI < cblCDName.Items.Count; cdI++)
                        {
                            if (this.cblCDName.Items[cdI].Selected)
                            {
                                if (this.cblCDName.Items[cdI].Value == DT.Rows[dtI]["PowerId"].ToString())
                                {
                                    modelRoleHavePower.PowerId = ChinaSoft.Common.Input.SetInt(cblCDName.Items[cdI].Value);
                                    modelRoleHavePower.RoleId = ChinaSoft.Common.Input.SetInt(Request.QueryString["id"]);
                                    modelRoleHavePower.CreateTime = DateTime.Now;
                                    modelRoleHavePower.CreateUserId = 0;
                                    if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                                        modelRoleHavePower.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                                    int RtnCDBool = bllRoleHavePower.Add(modelRoleHavePower);
                                }
                            }
                        }
                    }
                }
            }
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }

        protected void lbtAllMK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblMKName.Items.Count; i++)
            {
                cblMKName.Items[i].Selected = true;
            }

            DataCollectiveQX DColl = RecordRoleSet();
            InitCD();

            for (int i = 0; i < DColl.CountCD; i++)
            {
                for (int cdI = 0; cdI < cblCDName.Items.Count; cdI++)
                {
                    if (cblCDName.Items[cdI].Value == DColl.CDID[i].Trim())
                        cblCDName.Items[cdI].Selected = true;
                }
            }
        }

        protected void lbtESCMK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblMKName.Items.Count; i++)
            {
                cblMKName.Items[i].Selected = false;
            }

            DataCollectiveQX DColl = RecordRoleSet();
            InitCD();

            for (int i = 0; i < DColl.CountCD; i++)
            {
                for (int cdI = 0; cdI < cblCDName.Items.Count; cdI++)
                {
                    if (cblCDName.Items[cdI].Value == DColl.CDID[i].Trim())
                        cblCDName.Items[cdI].Selected = true;
                }
            }
        }

        protected void lbtAllCD_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblCDName.Items.Count; i++)
            {
                cblCDName.Items[i].Selected = true;
            }
        }

        protected void lbtESCCD_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cblCDName.Items.Count; i++)
            {
                cblCDName.Items[i].Selected = false;
            }
        }
    }
}